package com.mjh.comm.page;

import java.util.List;
import org.apache.struts2.json.annotations.JSON;

public class Pager<T> {

	private static int displayNum = 6;
	private static int edgeNum = 2;
	/**
	 * 每页条数
	 */
	private int pageSize = 20;
	/**
	 * 第几页
	 */
	private int pageNo;
	/**
	 * 总条数
	 */	
	private int totalNum;
	/**
	 * 每页的数据
	 */
	private List<T> pageData;
	/**
	 * 请求分页的url
	 */
	private String pageUrl;
	/**
	 * 保存构造的分页html
	 */
	private StringBuffer sb;
	
	public Pager(){
		
	}
	
	public Pager(int pageSize, int pageNo, int totalNum){
		this.pageSize = pageSize;
		this.pageNo = pageNo;
		this.totalNum = totalNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		if(pageNo < 1){
			pageNo = 1;
		}
		this.pageNo = pageNo;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}
	/*
	 * 总页数
	 */	
	public int getTotalPage() {
		if(pageSize != 0 && totalNum != 0){
			return (totalNum + pageSize - 1 ) / pageSize;
		} else {
			return 0;
		}
	}
	/*
	 * 当前页对应的开始记录号
	 */	
	public int getStartNo() {
		return (pageNo - 1) * pageSize; 
	}

	public List<T> getPageData() {
		return pageData;
	}

	public void setPageData(List<T> pageData) {
		this.pageData = pageData;
	}
	
	@JSON(serialize=false) 
	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	
	@JSON(serialize=false) 
	public String getPageHtml(){
		sb = new StringBuffer();
		if(this.getTotalPage() <= 1) return "";
		sb.append("<ul class='pagination sabrosus'>");
		createPrev();
		int[] pageRange = getPageRange();
		int begin,end;
		//最左边页数
		if (pageRange[0] > 1) {
			end = Math.min(edgeNum, pageRange[0]);
			renderRange(1, end);
			if(edgeNum < pageRange[0])
			{
				sb.append("<li>...</li>");
			}
		}
		//中间页数
		renderRange(pageRange[0], pageRange[1]);
		//最右边页数
		if (pageRange[1] <= this.getTotalPage()) {
			if(this.getTotalPage() - edgeNum > pageRange[1] ){
				sb.append("<li>...</li>");
			}
			begin = Math.max(this.getTotalPage() - edgeNum, pageRange[1]);
			renderRange(begin + 1, this.getTotalPage());
		}
		createNext();
		sb.append("</ul>");
		return sb.toString();
	}
	
	//下面的方法用于构造分页html
	private void createPageLink(int pageLinkNo, String linkText){
		sb.append("<li><a href='");
		sb.append(this.getPageUrl().replace("#pageNo#", String.valueOf(pageLinkNo)));
		sb.append("'>");
		sb.append(linkText);
		sb.append("</a></li>");
	}
	
	private void createPageItem(int pageNo){
		if(this.getPageNo() == pageNo){
			sb.append("<li class='current'><span>");
			sb.append(pageNo);
			sb.append("</span></li>");
		} else {
			createPageLink(pageNo, String.valueOf(pageNo));
		}
	}
	
	private void createPrev(){
		if(this.getPageNo() == 1){
			sb.append("<li class='disabled'><span>上一页</span></li>");
		} else {
			createPageLink(pageNo - 1, "上一页");
		}
	};
	
	private void createNext(){
		if(this.getPageNo() == this.getTotalPage()){
			sb.append("<li class='disabled'><span>下一页</span></li>");
		} else {
			createPageLink(pageNo + 1, "下一页");
		}
	};
	
	private int[] getPageRange(){
		int ne_half = displayNum/2;
		int np = this.getTotalPage();
		int upper_limit = np - displayNum;
		int start = this.getPageNo() > ne_half ? Math.max( Math.min(this.getPageNo() - ne_half, upper_limit), 1 ) : 1;
		int end =  this.getPageNo() > ne_half ? Math.min( this.getPageNo()+ne_half + (displayNum % 2), np) : Math.min(displayNum, np);
		return new int[]{start, end};
	};
	
	private void renderRange(int startNo, int endNo){
		for(int i=startNo;i<=endNo;i++){
			createPageItem(i);
		}
	};
}
